
int Precedence (char opera)
{
    if ('+' == opera || '-' == opera) return 1;
    else if ('*' == opera || '/' == opera) return 2;
    else if ('(' == opera) return 3;
    else return 0;
}

void ClearLastOpenBracket(char arrStack[], int* topStack)
{
    int i, j, len = strlen(arrStack);

    i=len-1;

    while('(' != arrStack[i] && 0<=i)
    {
        i--;
    }

    for (j = i; j<len; j++)
    {
        //printf("here!\n");
        arrStack[j] = arrStack[j+1];
    }
    (*topStack)--;
}

void Infix(char inputString[], char expression[30][15])
{
    int i=0, topExpression=0, len = strlen(inputString);
    while(i<len)
    {
        if(inputString[i] == '+' || inputString[i] == '-' || inputString[i] == '*' || inputString[i] == '/' || inputString[i] == '(' || inputString[i] == ')' )
        {
            expression[topExpression][0] = inputString[i];
            topExpression++;
            i++;
        }

        else if (inputString[i]>47 && inputString[i]<58 )
        {
            int j;
            for ( j = i ; (inputString[j]>47 && inputString[j]<58) || (inputString[j]=='.') ; j++ )
            {
                expression[topExpression][j-i] = inputString[j];
            }
            topExpression++;
            i=j;
        }
    }
}

void ConvertToPostfix(char expression[30][15], char postfix[30][15])
{
    int i, topStack = -1, topPostfix = -1;
    char arrStack[20];

    for(i = 0; i<20; i++)
    {
        arrStack[i] = '\0';
    }
    i = 0;

    while (expression[i][0]!='\0')
    {
        if (expression[i][0] >47 && expression[i][0] < 58/* && stack[0] == '\0'*/)
        {
            topPostfix++;
            int j;
            for (j = 0; expression[i][j] != '\0'; j++)
            {
                postfix[topPostfix][j] = expression[i][j];
            }

            i++;
        }

        if ( '+' == expression[i][0] || '-' == expression[i][0] || '*' == expression[i][0] || '/' == expression[i][0] )
        {
            // THE FIRST OPERATOR

            if ( -1 == topStack )
            {
                topStack ++;
                arrStack[topStack] = expression[i][0];

            }

            // ELSE
            else
            {
                // Initialise scanned
                char scanned = expression[i][0];

                // IF SCANNED OPERATOR > TOP-STACK OPERATOR
                if(Precedence(scanned) > Precedence(arrStack[topStack]))
                {
                    topStack++;
                    arrStack[topStack] = scanned;
                }

                // IF SCANNED OPERATOR = TOP-STACK OPERATOR
                else if (Precedence(scanned) == Precedence(arrStack[topStack]))
                {
                    topPostfix++;
                    postfix[topPostfix][0] = arrStack[topStack];
                    arrStack[topStack] = scanned;
                }

                // IF SCANNED OPERATOR < TOP-STACK OPERATOR
                //if(Precedence(scanned) < Precedence(arrStack[topStack]))
                else if (Precedence(scanned) == Precedence(arrStack[topStack]) && '(' != arrStack[topStack] )
                {
                    while (Precedence(arrStack[topStack]) > Precedence(arrStack[topStack-1]) && topStack > -1 )
                    {
                        topPostfix++;
                        postfix[topPostfix][0] = arrStack[topStack];
                        arrStack[topStack] = '\0';
                        topStack--;
                    }
                    topStack ++;
                    arrStack[topStack] = scanned;
                }

            }

            i++;
        }

        // OPEN BRACKET
        if ( '(' == expression[i][0] )
        {
            topStack++;
            arrStack[topStack] = '(';
            i++;
        }

        // CLOSE BRACKET
        if( ')' == expression[i][0] )
        {
            ClearLastOpenBracket(arrStack, &topStack);
            i++;
        }
    }

    int len = strlen(arrStack);
    for ( ; topStack >= 0 ; )
    {
        topPostfix++;
        postfix[topPostfix][0] = arrStack[topStack];
        arrStack[i] = '\0';
        topStack--;
    }
}

void calculating(char inputString[])
{
    char expression[30][15], postfix[30][15];

    int i,j;
    for(i = 0; i< 30; i++)
    {
        for(j = 0; j< 15; j++)
        {
            expression[i][j] = '\0';
            postfix[i][j] = '\0';
        }
    }

    Infix(inputString, expression);
    ConvertToPostfix(expression, postfix);


    for(i = 0; postfix[i][0] != '\0'; i++)
    {
        for(j = 0; postfix[i][j] != '\0'; j++)
        {
            printf("%c", postfix[i][j]);
        }
        printf("\n");
    }
}
